''' Data collection functions.

A bunch of functions that take no parameters, and return a short string
containing interesting data about a specific statistic. Examples: uptime, disk
space, average load, whether Java is installed, top running processes...

This data will eventually be displayed in a big HTML table for all the nodes:
                space(bytes)     uptime(days)  ...
pluto.com       23231/43523                51   
mars.edu        43235/32423                27
venus.ca        32652/23652                63
...
'''

import commands

def disk_space(disk):   
    'Returns the number of bytes used/available'
    #disk = commands.getoutput('df -P')
    disk = disk.splitlines()
    index = 0
    for item in disk:
        disk[index] = item.split()
        index += 1
    return disk[2][2] + "/" + disk[2][3]

def uptime(info):
    'Returns the number of days the system has been running.'
    #info = commands.getoutput('uptime')
    info = info.split()
    uptime = info[2]
    return uptime

def cpu_load(uptime):
    'Returns 15-minute load average normalized for any number of cores.'

    # find 15 minute load average by parsing uptime command
    #uptime = commands.getoutput('uptime')
    return float(uptime.rsplit(None, 1)[-1])
    
def num_cores(cores_info):
    'Returns the total number of cores'
    #cores_info = commands.getoutput('grep "cpu cores" /proc/cpuinfo')
    total_cores = 0
    for line in cores_info.splitlines():
        # each line is for a separate CPU
	  if line.find(":") > 0:
        	key,value = line.split(':')
        	total_cores += int(value.strip())
    if total_cores ==0:
	total_cores = 1
    return total_cores

def num_cpus(cpu_info):
    'Returns the total number of processors'
    #cpu_info = commands.getoutput('grep "processor" /proc/cpuinfo')
    cpu_info = cpu_info.rsplit(None, 1)
    return int(cpu_info[1]) + 1

def avg_cpu_freq(freq_info):
    'Returns the average processor frequency in Mhz'
    #freq_info = commands.getoutput('grep "cpu MHz" /proc/cpuinfo')
    freq_info = freq_info.splitlines()
    total_freq = 0
    for line in freq_info:
        # each line is for a separate CPU
	  if line.find(":") > 0:
        	key,value = line.split(':')
        	total_freq += float(value.strip())
    return int(total_freq / len(freq_info))

def boot_time(boot_time):
    'returns the inital system boot time'
    #boot_time = commands.getoutput('who -b')
    boot_time = boot_time.split()
    return boot_time[2]+" "+boot_time[3]
    
def memory(mem_info):
    'Returns the number of MBs of memory used/available'
    #mem_info = commands.getoutput('free -m')
    mem_info = mem_info.splitlines()[1].split()
    return mem_info[2] + "/" + mem_info[3]

def java(java_info):
    'Returns the Java version number if it has been installed'
    'or None if it has not been'
    #java_info = commands.getoutput('java -version')
    java_info = java_info.splitlines()[0].split()
    if java_info[1] == "version":
        return java_info[2].strip('"')
    else:
        return None